package com.chandra.pos.product.repository;

import com.chandra.pos.SearchCriteria;
import com.chandra.pos.product.dao.entity.Product;
import com.chandra.pos.product.model.ProductSearch;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Customer: T4671
 * Date: 10/5/13
 * Time: 12:39 PM
 * To change this template use File | Settings | File Templates.
 */
public class ProductRepositoryImpl implements ProductRepositoryCustom {

    @PersistenceContext
    private EntityManager entityManager;


    @Override
    public List<Product> search(ProductSearch searchCriteria) {


        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        CriteriaQuery<Product> criteriaQuery=cb.createQuery(Product.class) ;

        Root<Product> userRoot = criteriaQuery.from(Product.class);

        List<Predicate> predicates = new ArrayList<Predicate>();


        if (searchCriteria.getName() != null)
        {

            predicates.add(cb.like(cb.lower(userRoot.<String>get("name")),
                    "%" + searchCriteria.getName().toLowerCase() + "%"));


        }


        if (searchCriteria.getDescription() != null && !searchCriteria.getDescription().isEmpty()) {
            predicates.add(cb.like(cb.lower(userRoot.<String>get("description")),
                    "%" + searchCriteria.getDescription().toLowerCase() + "%"));

        }

        if (searchCriteria.getStatus() != null)
            predicates.add(cb.equal(userRoot.get("sStatus"),searchCriteria.getStatus()));



        if (searchCriteria.getSalePrice() != null)
            predicates.add(cb.equal(userRoot.get("salePrice"),searchCriteria.getSalePrice()));





        criteriaQuery.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
        criteriaQuery.distinct(true);

        return entityManager.createQuery(criteriaQuery).getResultList();


    }
    @Override
    public List<Product> search(SearchCriteria searchCriteria) {


        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Product> criteriaQuery=criteriaBuilder.createQuery(Product.class) ;

        Root<Product> userRoot = criteriaQuery.from(Product.class);

        List<Predicate> predicates = new ArrayList<Predicate>();


        if(searchCriteria.getFilters() !=null)
        for (final Map.Entry<String, String> e : searchCriteria.getFilters().entrySet())
        {

            final String key = e.getKey();
            final String value = e.getValue();

            if ((key != null) && (value != null)) {

                if (value.contains("%")) {
                    predicates.add(criteriaBuilder.like(userRoot.<String> get(key), value));
                } else {
                    predicates.add(criteriaBuilder.equal(userRoot.get(key), value));
                }
            }
        }

        criteriaQuery.where(criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()])));
        criteriaQuery.distinct(true);

        return entityManager.createQuery(criteriaQuery).setFirstResult(searchCriteria.getFirst()).setMaxResults(searchCriteria.getPageSize()).getResultList();


    }

}
